package com.dxq.wan;

import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.dxq.basic_ui_library.loadSirCallBack.EmptyCallback;
import com.dxq.basic_ui_library.loadSirCallBack.ErrorCallback;
import com.dxq.basic_ui_library.loadSirCallBack.LoadingCallback;
import com.dxq.basic_ui_library.loadSirCallBack.LottieLoadingCallback;
import com.dxq.wan.api.Api;
import com.dxq.wan.api.ApiService;
import com.dxq.wan.bean.LoginResult;
import com.kingja.loadsir.callback.Callback;
import com.kingja.loadsir.core.LoadService;
import com.kingja.loadsir.core.LoadSir;

import retrofit2.Call;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class Main3Activity extends AppCompatActivity {

    private LoadService loadService;
    private TextView tv_content;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
        initView();
        initLoadSir2();
        loadNet();

    }

    private void initView() {
        tv_content = (TextView) this.findViewById(R.id.tv_content);
    }

    private void initLoadSir() {
        // Your can change the callback on sub thread directly.
        LoadSir loadSir = new LoadSir.Builder()
                .addCallback(new EmptyCallback())
                .addCallback(new ErrorCallback())
                .addCallback(new LottieLoadingCallback())
                .setDefaultCallback(LottieLoadingCallback.class)
                .build();
        loadService = loadSir.register(this, new Callback.OnReloadListener() {
            @Override
            public void onReload(View v) {
                // Your can change the status out of Main thread.
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        loadService.showCallback(LottieLoadingCallback.class);
                        //do retry logic...
                        SystemClock.sleep(500);
                        //callback on sub thread
                        loadService.showSuccess();//显示加载成功
                    }
                }).start();
            }
        });
    }

    private void initLoadSir2() {
        loadService = LoadSir.getDefault().register(this, new Callback.OnReloadListener() {
            @Override
            public void onReload(View v) {
                // Your can change the status out of Main thread.
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        loadService.showCallback(LoadingCallback.class);
//                        //do retry logic...
//                        SystemClock.sleep(500);
//                        //callback
//                        loadService.showSuccess();
                        loadNet();
                    }
                }).start();
            }
        });
    }

    private void loadNet() {
        //PostUtil.postCallbackDelayed(loadService, EmptyCallback.class, 2000);//模拟网络加载
//        EasyHttp.post("/user/login").baseUrl("https://www.wanandroid.com")
//                .params("username", "zsh").params("password", "123456")
//                .execute(new SimpleCallBack<String>() {
//                    @Override
//                    public void onError(ApiException e) {
//                        loadService.showCallback(ErrorCallback.class);
//                    }
//
//                    @Override
//                    public void onSuccess(String s) {
//                        loadService.showSuccess();
//                        tv_content.setText(s);
//                    }
//                });
//        Retrofit retrofit = new Retrofit
//                .Builder()
//                .baseUrl(Api.baseUrl)
//                .addConverterFactory(GsonConverterFactory.create())
//                .build();

//        ApiService apiService = retrofit.create(ApiService.class);
        Call<LoginResult> loginCall = App.apiService.login("zsh", "123456");
        loginCall.enqueue(new retrofit2.Callback<LoginResult>() {
            @Override
            public void onResponse(Call<LoginResult> call, Response<LoginResult> response) {
                loadService.showSuccess();
                tv_content.setText(response.body().getData().getPublicName());
            }

            @Override
            public void onFailure(Call<LoginResult> call, Throwable t) {
                loadService.showCallback(ErrorCallback.class);
            }
        });
    }
}
